package com.qiankun.tang.ui.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.common.library.ui.activity.BaseActivity;
import com.qiankun.tang.R;
import com.qiankun.tang.ui.fragment.find.FindFragment;
import com.qiankun.tang.ui.fragment.follow.FollowFragment;
import com.qiankun.tang.ui.fragment.home.HomeFragment;
import com.qiankun.tang.ui.fragment.mine.MineFragment;
import com.qiankun.tang.ui.view.TabBar;

import java.util.List;

public class MainActivity extends BaseActivity implements TabBar.OnTabCheckedChangeListener {
    TabBar mTabBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    private void init() {
        mTabBar = getViewById(R.id.activity_main_TabBar);
        mTabBar.setOnTabCheckedChangeListener(this);
        mTabBar.getTab1().performClick();
    }
    @Override
    public void onCheckedChanged(@IdRes int checkedId) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        List<Fragment> fragments = manager.getFragments();
        if (fragments != null) {
            for (Fragment f : fragments) {
                transaction.hide(f);
            }
        }
        switch (checkedId) {
            case R.id.tab1://主页
                selectedTab1(manager, transaction);
                break;
            case R.id.tab2://发现
                selectedTab2(manager, transaction);
                break;
            case R.id.tab3://关注
                selectedTab3(manager, transaction);
                break;
            case R.id.tab4://我的
                selectedTab4(manager, transaction);
                break;
        }
        transaction.commit();
    }
    private void selectedTab1(FragmentManager manager, FragmentTransaction transaction) {
        if (manager.findFragmentByTag("HomeFragment") == null) {
            transaction.add(R.id.activity_main_content, new HomeFragment(), "HomeFragment");
        } else {
            transaction.show(manager.findFragmentByTag("HomeFragment"));
        }
    }
    private void selectedTab2(FragmentManager manager, FragmentTransaction transaction) {
        if (manager.findFragmentByTag("FindFragment") == null) {
            transaction.add(R.id.activity_main_content, new FindFragment(), "FindFragment");
        } else {
            transaction.show(manager.findFragmentByTag("FindFragment"));
        }
    }
    private void selectedTab3(FragmentManager manager, FragmentTransaction transaction) {
        if (manager.findFragmentByTag("FollowFragment") == null) {
            transaction.add(R.id.activity_main_content, new FollowFragment(), "FollowFragment");
        } else {
            transaction.show(manager.findFragmentByTag("FollowFragment"));
        }
    }
    private void selectedTab4(FragmentManager manager, FragmentTransaction transaction) {
        if (manager.findFragmentByTag("MineFragment") == null) {
            transaction.add(R.id.activity_main_content, new MineFragment(), "MineFragment");
        } else {
            transaction.show(manager.findFragmentByTag("MineFragment"));
        }
    }

}
